/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.close.basic.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.csp.sentinel.util.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.close.basic.dto.ClosedGpsDeviceDTO;
import com.hopes.bizz.common.core.util.QRCodeUtils;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.log.annotation.SysLog;
import com.hopes.bizz.close.basic.entity.ClosedGpsDeviceEntity;
import com.hopes.bizz.close.basic.dto.ClosedGpsDeviceExcelDTO;
import com.hopes.bizz.close.basic.vo.ClosedGpsDeviceExcelVO;
import com.hopes.bizz.close.basic.service.ClosedGpsDeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import com.hopes.boss.common.excel.annotation.RequestExcel;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;

/**
 * GPS设备
 *
 * @author pig
 * @date 2023-10-24 17:17:29
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/gpsdevice")
@Tag(description = "gpsdevice", name = "GPS设备管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ClosedGpsDeviceController {

    private final ClosedGpsDeviceService closedGpsDeviceService;

    /**
     * 分页查询
     *
     * @param page               分页对象
     * @param closedGpsDeviceDTO GPS设备
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('basic_gpsdevice_view')")
    public R getClosedGpsDevicePage(Page page, ClosedGpsDeviceDTO closedGpsDeviceDTO) {
        final LambdaQueryWrapper<ClosedGpsDeviceEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StrUtil.isNotBlank(closedGpsDeviceDTO.getUseFlag()), ClosedGpsDeviceEntity::getUseFlag, closedGpsDeviceDTO.getUseFlag());
        wrapper.eq(StrUtil.isNotBlank(closedGpsDeviceDTO.getBindFlag()), ClosedGpsDeviceEntity::getBindFlag, closedGpsDeviceDTO.getBindFlag());
        wrapper.eq(StrUtil.isNotBlank(closedGpsDeviceDTO.getDeviceStatus()), ClosedGpsDeviceEntity::getDeviceStatus, closedGpsDeviceDTO.getDeviceStatus());
        wrapper.eq(StrUtil.isNotBlank(closedGpsDeviceDTO.getNetworkStatus()), ClosedGpsDeviceEntity::getNetworkStatus, closedGpsDeviceDTO.getNetworkStatus());
        wrapper.like(StrUtil.isNotBlank(closedGpsDeviceDTO.getKeyword()), ClosedGpsDeviceEntity::getDeviceName, closedGpsDeviceDTO.getKeyword())
                .or().like(StrUtil.isNotBlank(closedGpsDeviceDTO.getKeyword()), ClosedGpsDeviceEntity::getDeviceNo, closedGpsDeviceDTO.getKeyword())
                .or().like(StrUtil.isNotBlank(closedGpsDeviceDTO.getKeyword()), ClosedGpsDeviceEntity::getLogisticsBillNo, closedGpsDeviceDTO.getKeyword());
        return R.ok(closedGpsDeviceService.page(page, wrapper));
    }


    /**
     * 通过id查询GPS设备
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('basic_gpsdevice_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(closedGpsDeviceService.getById(id));
    }


    /**
     * 查看GPS设备二维码
     * @param id
     * @param response
     */
    @Operation(summary = "查看Gps设备二维码" , description = "查看Gps设备二维码" )
    @GetMapping(value = "qrInfo/{id}")
    @PreAuthorize("@pms.hasPermission('basic_gpsdevice_view')" )
    public void info(@PathVariable("id" ) String id, HttpServletResponse response) {
        try {
            final ClosedGpsDeviceEntity entity = closedGpsDeviceService.getById(id);
            ServletOutputStream out = response.getOutputStream();
            if (StringUtil.isNotBlank(entity.getDeviceNo())) {
                QRCodeUtils.encode(entity.getDeviceNo(), out);
            } else {
                QRCodeUtils.encode(String.valueOf(entity.getId()), out);
            }
        } catch (Exception ex) {
            log.error("查看Gps设备二维码", ex);
        }
    }

    /**
     * 新增GPS设备
     *
     * @param closedGpsDevice GPS设备
     * @return R
     */
    @Operation(summary = "新增GPS设备", description = "新增GPS设备")
    @SysLog("新增GPS设备")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('basic_gpsdevice_add')")
    public R save(@RequestBody ClosedGpsDeviceEntity closedGpsDevice) {
        return R.ok(closedGpsDeviceService.save(closedGpsDevice));
    }

    /**
     * 修改GPS设备
     *
     * @param closedGpsDevice GPS设备
     * @return R
     */
    @Operation(summary = "修改GPS设备", description = "修改GPS设备")
    @SysLog("修改GPS设备")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('basic_gpsdevice_edit')")
    public R updateById(@RequestBody ClosedGpsDeviceEntity closedGpsDevice) {
        return R.ok(closedGpsDeviceService.updateById(closedGpsDevice));
    }

    /**
     * 通过id删除GPS设备
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除GPS设备", description = "通过id删除GPS设备")
    @SysLog("通过id删除GPS设备")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('basic_gpsdevice_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(closedGpsDeviceService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * excel模板
     *
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/template")
    @PreAuthorize("@pms.hasPermission('basic_gpsdevice_export')")
    public List<ClosedGpsDeviceExcelDTO> template() {
        return Collections.singletonList(new ClosedGpsDeviceExcelDTO());
    }

    /**
     * 导出excel 表格
     *
     * @param closedGpsDeviceDTO 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('basic_gpsdevice_export')")
    public List<ClosedGpsDeviceExcelVO> export(ClosedGpsDeviceDTO closedGpsDeviceDTO) {
        final LambdaQueryWrapper<ClosedGpsDeviceEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StrUtil.isNotBlank(closedGpsDeviceDTO.getUseFlag()), ClosedGpsDeviceEntity::getUseFlag, closedGpsDeviceDTO.getUseFlag());
        wrapper.eq(StrUtil.isNotBlank(closedGpsDeviceDTO.getDeviceStatus()), ClosedGpsDeviceEntity::getDeviceStatus, closedGpsDeviceDTO.getDeviceStatus());
        wrapper.eq(StrUtil.isNotBlank(closedGpsDeviceDTO.getNetworkStatus()), ClosedGpsDeviceEntity::getNetworkStatus, closedGpsDeviceDTO.getNetworkStatus());
        wrapper.like(StrUtil.isNotBlank(closedGpsDeviceDTO.getKeyword()), ClosedGpsDeviceEntity::getDeviceName, closedGpsDeviceDTO.getKeyword())
                .or().like(StrUtil.isNotBlank(closedGpsDeviceDTO.getKeyword()), ClosedGpsDeviceEntity::getDeviceNo, closedGpsDeviceDTO.getKeyword())
                .or().like(StrUtil.isNotBlank(closedGpsDeviceDTO.getKeyword()), ClosedGpsDeviceEntity::getLogisticsBillNo, closedGpsDeviceDTO.getKeyword());
        return closedGpsDeviceService.exportExcel(closedGpsDeviceService.list(wrapper));
    }

    /**
     * 导入excel 表格
     *
     * @param closedGpsDeviceExcelDtoList 查询条件
     * @param bindingResult               错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('basic_gpsdevice_export')")
    public R importExcel(@RequestExcel List<ClosedGpsDeviceExcelDTO> closedGpsDeviceExcelDtoList, BindingResult bindingResult) {
        return closedGpsDeviceService.importExcel(closedGpsDeviceExcelDtoList, bindingResult);
    }
}
